#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Example showing how to create and tick a simple behaviour tree."""
import time
import functools
import py_trees


def post_tick_handler(snapshot_visitor, behaviour_tree):
    print(
        py_trees.display.unicode_tree(
            behaviour_tree.root,
            visited=snapshot_visitor.visited,
            previously_visited=snapshot_visitor.visited,
        )
    )


def create_tree() -> py_trees.behaviour.Behaviour:
    root = py_trees.composites.Sequence(name="Sequence", memory=True)
    for action in ["Action 1", "Action 2", "Action 3"]:
        b = py_trees.behaviours.StatusQueue(
            name=action,
            queue=[py_trees.common.Status.RUNNING],
            eventually=py_trees.common.Status.SUCCESS,
        )
        root.add_child(b)
    return root


def main() -> None:
    root = create_tree()
    behaviour_tree = py_trees.trees.BehaviourTree(root)
    """
    tick滴嗒后打印模板
    """
    snapshot_visitor = py_trees.visitors.SnapshotVisitor()
    behaviour_tree.add_post_tick_handler(functools.partial(post_tick_handler, snapshot_visitor))
    behaviour_tree.visitors.append(snapshot_visitor)

    for i in range(1, 7):
        try:
            print("\n--------- Tick {0} ---------\n".format(i))
            behaviour_tree.tick()
            time.sleep(1.0)
        except KeyboardInterrupt:
            break


if __name__ == "__main__":
    main()
